package com.gitee.somenvm.ninerpgitem.core.kether

import org.bukkit.entity.Player
import taboolib.common.platform.function.adaptPlayer
import taboolib.common5.Coerce
import taboolib.library.configuration.ConfigurationSection
import taboolib.module.kether.KetherShell.eval
import taboolib.module.kether.ScriptOptions

const val NAMESPACE = "NineItem"

val namespaces = listOf(NAMESPACE, "kether")

/**
 * 不会全部运行,比较所有Ks语句
 *
 *
 * 格式为:
 * - condition: 比较脚本
 *   deny: 失败脚本
 * - condition: 比较脚本
 *   deny: 失败脚本
 *
 */
fun ConfigurationSection.conditionAllKs(path: String, player: Player): Boolean {
    this.getMapList(path).forEach {
        val context = it
        try {
            {}
            val c = eval(
                Coerce.toString(context["condition"] ?: "true"), ScriptOptions.builder().namespace(emptyList()).sender(
                    sender = adaptPlayer(player)
                ).build()
            ).getNow(false)
            if (c != true) {
                context["deny"]?.let { script ->
                    try {
                        eval(
                            Coerce.toString(script),
                            ScriptOptions.builder().namespace(emptyList()).sender(sender = adaptPlayer(player)).build()
                        )
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                }
                return false
            }
        } catch (e: Exception) {
            e.printStackTrace()
            return false
        }
    }
    return true
}